package com.hm.shopping.def.su;

import java.text.MessageFormat;

import com.hm.common.exception.ServiceException;
import com.hm.common.util.CommonUtil;
import com.hm.shopping.def.OrderStateEnum;

/**
 * @author shishun.wang
 * @date 2018年1月22日 下午3:02:23
 * @version 1.0
 * @describe 订单状态状态机
 */
public enum OrderStateMachineEngine {

	/**
	 * @author shishun.wang
	 * @date 2018年1月22日 下午3:46:44
	 * @version 1.0
	 * @describe 下单
	 */
	ORDER_INIT {

		@Override
		public OrderStateEnum conversion(OrderStateEnum currentOrderState) {
			if (CommonUtil.isNotEmpty(currentOrderState)) {
				throw ServiceException.warn(MessageFormat.format("订单状态【{0}】不匹配", currentOrderState));
			}
			return OrderStateEnum.STAY_PAYMENT;
		}
	},

	/**
	 * @author shishun.wang
	 * @date 2018年1月22日 下午3:50:48
	 * @version 1.0
	 * @describe 订单支付
	 */
	ORDER_PAYMENT {

		@Override
		public OrderStateEnum conversion(OrderStateEnum currentOrderState) {
			if (currentOrderState != OrderStateEnum.STAY_PAYMENT) {
				throw ServiceException.warn(MessageFormat.format("订单状态【{0}】不匹配", currentOrderState));
			}
			return OrderStateEnum.FINISH_PAYMENT;
		}

	},

	/**
	 * @author shishun.wang
	 * @date 2018年1月22日 下午4:06:36
	 * @version 1.0
	 * @describe 发货
	 */
	SEND_COURIER {

		@Override
		public OrderStateEnum conversion(OrderStateEnum currentOrderState) {
			if (currentOrderState != OrderStateEnum.FINISH_PAYMENT) {
				throw ServiceException.warn(MessageFormat.format("订单状态【{0}】不匹配", currentOrderState));
			}

			return OrderStateEnum.FINISH_COURIER;
		}

	},

	/**
	 * @author shishun.wang
	 * @date 2018年1月22日 下午3:57:13
	 * @version 1.0
	 * @describe 结束订单(收货)，超时确认已收货或人工触发已收货
	 */
	ORDER_FINISH {

		@Override
		public OrderStateEnum conversion(OrderStateEnum currentOrderState) {
			if (CommonUtil.isEmpty(currentOrderState)) {
				throw ServiceException.warn(MessageFormat.format("订单状态【{0}】不匹配", currentOrderState));
			}
			if (currentOrderState != OrderStateEnum.FINISH_COURIER) {
				throw ServiceException.warn(MessageFormat.format("订单状态【{0}】不匹配", currentOrderState));
			}

			return OrderStateEnum.FINISH;
		}

	},

	/**
	 * @author shishun.wang
	 * @date 2018年1月22日 下午3:54:08
	 * @version 1.0
	 * @describe 订单取消
	 */
	ORDER_CANCEL {

		@Override
		public OrderStateEnum conversion(OrderStateEnum currentOrderState) {
			if (currentOrderState != OrderStateEnum.STAY_PAYMENT) {
				throw ServiceException.warn(MessageFormat.format("订单状态【{0}】不匹配", currentOrderState));
			}

			return OrderStateEnum.FINISH;
		}

	};

	public abstract OrderStateEnum conversion(OrderStateEnum currentOrderState);
}
